<template>
    <el-form ref="ruleFormRef" style="width: 100%" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon >
        <el-form-item label="逾期时间" prop="slippage">
            <el-select v-model="ruleForm.days">
                <el-option label="请选择" :value="0" />
                <el-option label="逾期一天" :value="1" />
                <el-option label="逾期二天" :value="2" />
                <el-option label="逾期三天" :value="3" />
                <el-option label="逾期四天" :value="4" />
                <el-option label="逾期五天" :value="5" />
                <el-option label="逾期六天" :value="6" />
                <el-option label="逾期七天" :value="7" />
            </el-select>
        </el-form-item>
        <el-form-item label="检查类别" prop="type_id">
            <component :is="typeSelect" :data=ruleForm.type_id @onTypeSelect="onTypeSelect" />
        </el-form-item>
        <el-form-item label="责任人" prop="user_id">
            <component :is="userSelect" :data=ruleForm.user_id @onUserSelect="onUserSelect" />
        </el-form-item>

    </el-form>
    <el-divider />
    <div class="text-center">
        <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="loading"> 保存 </el-button>
    </div>
</template>
  
<script lang="ts" setup>
import { reactive, ref,watch } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import typeSelect from "./typeSelect.vue";
import userSelect from "@/components/userSelect.vue";
import http from '../../../../util/request';
const loading = ref<boolean>(false)

interface RuleForm {
    id: number
    days: number
    type_id:number|string
    user_id: number|string
    module:string
}

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
    id: 0,
    days: 0,
    type_id:0,
    user_id: 0,
    module: '',
})

const onGetData = () => {
    http.get('/notify/'+ruleForm.value.id).then((response:any)=>{
        if(response.data.data){
            ruleForm.value = response.data.data
        }
    })
}

const props = defineProps<{
    data:number
    module:string
}>()

watch(props,()=>{
    ruleForm.value.id = props.data
    ruleForm.value.module = props.module
    if(!ruleForm.value.id){
        ruleForm.value.days = 0
        ruleForm.value.type_id = ''
        ruleForm.value.user_id = ''
    }
    onGetData()
},{ immediate: true })

const rules = reactive<FormRules<RuleForm>>({
    days: [
        { required: true, message: '请输入名称', trigger: 'blur' },
    ],
    type_id: [
        { required: true, message: '请选择检查类别', trigger: 'blur' },
    ],
    user_id: [
        { required: true, message: '请选择责任人', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
    console.log(ruleForm.value)
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            loading.value = true
            let request:any = null
            if(ruleForm.value.id){
                request = http.patch('/notify',ruleForm.value)
            }else{
                request = http.put('/notify',ruleForm.value)
            }
            request.then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);
                    return
                }
                onSave()
            })         
        } else {
            console.log('error submit!', fields)
        }
    })
}


const onTypeSelect = (data:number) => {
    ruleForm.value.type_id = data
}
const onUserSelect = (data:number) => {
    ruleForm.value.user_id = data
}
const onSave = () => {
    emit('onSave')
}

const emit = defineEmits(['onSave'])
</script>
<style scoped>
</style>
